//setting.js
var common = require("../../../../utils/common.js");
//获取应用实例
var app = getApp()
Page({
  data:{
    baseDataList:[{"name":"成员","describe":"编辑您的家庭成员","submenu":true},{"name":"支出类型","describe":"编辑您的支出类型","submenu":true},{"name":"收入类型","describe":"编辑您的收入类型","submenu":true},{"name":"卡种","describe":"编辑各种消费卡，用于记录刷卡消费","submenu":true}],
    otherDataList:[{"name":"账户绑定","describe":"绑定后，多个微信记同一账本","submenu":true}],
    userInfo: {},
    actionSheetList: ['绑定微信用户','绑定已注册用户' ]
  },
  onLoad: function () {
    var that = this
    if(!app.globalData.recordParam){
      app.getRecordParam(function(paramRes){
        // console.log(paramRes)
      })
    }
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      if((userInfo.nickName!=app.globalData.userInfo.nickName)||userInfo.avatarUrl!=app.globalData.userInfo.avatarUrl){
        app.globalData.userInfo.nickName=userInfo.nickName
        app.globalData.userInfo.avatarUrl=userInfo.avatarUrl
        //更新服务器端用户信息
      }
    })
    that.setData({
      userInfo:app.globalData.userInfo
    })
    //全局userInfo是否有bindInfo，有：直接添加list，无：发送请求，更新全局userInfo，再次判断有没有bindInfo，有：直接添加list，无：不做任何操作   
    if(app.globalData.userInfo.binduserinfo){
      that.setData({
        actionSheetList:['解除绑定']
      })
    }else{
      wx.request({
        url: common.apiUrl()+'getUserInfo',
        data: {
          token:app.globalData.wangcaiToken
        },
        success: function(res){
          if(res.data.status==-1){
            app.errorToGetToken()
          }
// 如果获取的用户信息中昵称和头像信息和本地存储的全局变量不同，修改获取信息中的昵称和头像信息，再同返回的binduserinfo信息一起赋值给本地存储
          if((res.data.data.nickName!=app.globalData.userInfo.nickName)||res.data.data.avatarUrl!=app.globalData.userInfo.avatarUrl){
            res.data.data.nickName=app.globalData.userInfo.nickName
            res.data.data.avatarUrl=app.globalData.userInfo.avatarUrl
          }

          app.globalData.userInfo = res.data.data
          that.setData({
            userInfo:app.globalData.userInfo
          })
          // console.log(that.data.userInfo)
          if(app.globalData.userInfo.binduserinfo){
            that.setData({
              actionSheetList:['解除绑定']
            })
          }
        }
      })
    }
  },
  baseItem:function(e) {
    wx.navigateTo({
              url: '/pages/smartlife/wangcai/setting/basedata/basedata?id='+e.currentTarget.dataset.index
            })
  },
  otherItem:function(e) {
    switch(e.currentTarget.dataset.index)
    {
      case 0:
        this.bindAccount()
        break;
      case 1:

        break;
      case 2:

        break;
      default:
      //
    }
  },
  headClick: function (e) {
    this.bindAccount()
  },
  bindAccount: function (e) {
    var that = this;
    wx.showActionSheet({
      itemList: that.data.actionSheetList,
      success: function(res) {
        // console.log(res)
        switch(res.tapIndex){
          case 0://绑定其他微信账号
            if(that.data.actionSheetList[0]=='绑定微信用户'){
              wx.navigateTo({
                url: '/pages/smartlife/wangcai/setting/bindwechat/bindwechat'
              })
            }else{
                wx.showModal({
                title: '警告',
                content: '解除与 '+that.data.userInfo.binduserinfo.nickName+' 的绑定后，您将无法查看与编辑 '+that.data.userInfo.binduserinfo.nickName+' 的记录，确定要解除绑定吗？',
                success: function(res) {
                  if (res.confirm) {
                    wx.showToast({
                      title: '加载中',
                      icon: 'loading'
                    })
                    wx.request({
                      url: common.apiUrl()+'unbind',
                      data: {
                        token:app.globalData.wangcaiToken
                      },
                      success: function(res){
                        if(res.data.status==1){
                          wx.hideToast()
                          wx.showToast({
                            title: '解绑成功',
                            icon: 'success'
                          })
                          // 取消解除绑定按钮
                          that.unbindCallback()
                          //刷新上一页数据
                          var pages = getCurrentPages();
                          if(pages.length > 1){
                              //上一个页面实例对象
                              var prePage = pages[pages.length - 2];
                              prePage.getData()
                          }
                          wx.navigateBack({
                            delta: 1
                          })
                        }else{
                          app.errorToGetToken()
                        }
                      }
                    })
                  }
                }
              })
            }
          break;
          case 1:
            wx.navigateTo({
              url: '/pages/smartlife/wangcai/setting/bind/bind'
            })
          break;
          case 2://解除绑定
            // console.log(that.data.userInfo)
            wx.showModal({
              title: '警告',
              content: '解除与 '+that.data.userInfo.binduserinfo.nickName+' 的绑定后，您将无法查看与编辑 '+that.data.userInfo.binduserinfo.nickName+' 的记录，确定要解除绑定吗？',
              success: function(res) {
                if (res.confirm) {
                  wx.showToast({
                    title: '加载中',
                    icon: 'loading'
                  })
                  wx.request({
                    url: common.apiUrl()+'unbind',
                    data: {
                      token:app.globalData.wangcaiToken
                    },
                    success: function(res){
                      if(res.data.status==1){
                        wx.hideToast()
                        wx.showToast({
                          title: '解绑成功',
                          icon: 'success'
                        })
                        // 取消解除绑定按钮
                        that.unbindCallback()
                        //刷新上一页数据
                        var pages = getCurrentPages();
                        if(pages.length > 1){
                            //上一个页面实例对象
                            var prePage = pages[pages.length - 2];
                            prePage.getData()
                        }
                        wx.navigateBack({
                          delta: 1
                        })
                      }else{
                        app.errorToGetToken()
                      }
                    }
                  })
                }
              }
            })
          break;
        }
      },
      fail: function(res) {

      }
    })
  },
  bindCallback: function (e) {
    var that = this
    that.setData({
        actionSheetList:['解除绑定'],
        // 刷新用户数据
        userInfo:app.globalData.userInfo
      })
      // 刷新记录参数数据recordParam
      app.getRecordParam(function(paramRes){
        // console.log(paramRes)
      })
  },
  unbindCallback: function (e) {
    var that = this
    that.setData({
      actionSheetList:['绑定已注册用户', '绑定其他微信用户']
    })
    // 刷新用户数据
    app.globalData.userInfo.binduserinfo = null
    // 刷新记录参数数据recordParam
    app.getRecordParam(function(paramRes){
      // console.log(paramRes)
    })
  },

})